package com.travelSpring.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travelSpring.pojo.entity.Scenic;
import com.travelSpring.pojo.vo.ScenicVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ScenicMapper extends BaseMapper<Scenic> {
    // 新增分页查询方法
    @Select("SELECT s.*, c.name AS category_name, v.name AS vendor_name ," +
            "(SELECT COUNT(*) FROM user_favorite WHERE scenic_id = s.id) as save_count, " +
            "(SELECT COUNT(*) FROM product_view WHERE scenic_id = s.id) as view_count " +
            "FROM scenic s " +
            "LEFT JOIN scenic_category c ON s.category_id = c.id " +
            "LEFT JOIN vendor v ON s.vendor_id = v.id " +
            "${ew.customSqlSegment}")
    @Results({
            @Result(column = "images", property = "images", typeHandler = JacksonTypeHandler.class),
            @Result(column = "end_point", property = "endPoint", typeHandler = JacksonTypeHandler.class),
            @Result(column = "start_point", property = "startPoint", typeHandler = JacksonTypeHandler.class),
            @Result(column = "category_name", property = "categoryName"),
            @Result(column = "vendor_name", property = "vendorName")
            // 其他字段如果名称一致，可以省略 @Result
    })
    IPage<ScenicVO> selectScenicWithPage(Page<Scenic> page,
                                         @Param(Constants.WRAPPER) LambdaQueryWrapper<Scenic> wrapper);

}
